package com.bdzl.ecs.util.file;

import com.bdzl.ecs.config.Constant;
import com.bdzl.ecs.util.string.StringUtils;
import com.bdzl.ecs.util.time.DateUtils;
import com.bdzl.pub.util.file.BASE64DecodedMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;
import java.util.UUID;

/**
 * @author yangdeyin
 * @方法参数 objType 路径前缀
 * @modifyTime 2020-8-31
 * @see #upLoad(String, InputStream)
 * @see #generatePath(String, String)
 * @see #remove(String)
 * @see #bathRemove(List)
 */
public class OssUploadFileUtil {

    /**
     * 上传文件
     *
     * @param path 上传文件的路径
     * @param is   文件流
     * @return
     */
    public static String upLoad(String path, InputStream is) {
        OSSClientFactory.uploadFile(path, is);
        return Constant.OSS_PIC_PREFIX + path;
    }

    /**
     * 生成路径
     *
     * @param fileName 文件名
     * @return
     */
    public static String generatePath(String objType, String fileName) {
        return objType + "/" + DateUtils.getCurDate() + "/" + fileName;
    }

    /**
     * 删除文件
     *
     * @param url 示例：https://bdzlbackoss.oss-cn-hangzhou.aliyuncs.com/plan/plan_files/2019-12-26/90ac92d9-83c6-41a2-91d7-eda8862de3be.jpg
     */
    public static void remove(String url) {
        if (url == null) return;
        try {
            OSSClientFactory.remove(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 批量删除文件
     *
     * @param urls 示例数组：https://bdzlbackoss.oss-cn-hangzhou.aliyuncs.com/plan/plan_files/2019-12-26/90ac92d9-83c6-41a2-91d7-eda8862de3be.jpg
     */
    public static void bathRemove(List<String> urls) {
        if (urls == null || urls.isEmpty()) return;
        try {
            OSSClientFactory.bathRemove(urls);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @author yangdeyin
     * @方法参数 这个类下的所有方法上传的阿里云文件, 文件名都是随机生成
     * @modifyTime 2020-8-11
     * @see #generateFileName(Object)
     * @see #uploadToOSS(MultipartFile, String)
     * @see #uploadToOSS(File, String)
     * @see #uploadToOSS(String, String)
     */
    public static class RandomFileName {
        /**
         * 根据文件流，生成一个当前扩展名的随机文件名
         *
         * @return
         */
        public static String generateFileName(Object file) {
            String fileName = "";
            if (file instanceof MultipartFile) {
                fileName = ((MultipartFile) file).getOriginalFilename();
            } else if (file instanceof File) {
                fileName = ((File) file).getName();
            } else if (file instanceof String) {
                fileName = (String) file;
            }
            return UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
        }

        public static String uploadToOSS(MultipartFile file, String objType) throws IOException {
            return upLoad(generatePath(objType, generateFileName(file)), file.getInputStream());
        }


        public static String uploadToOSS(File file, String objType) throws FileNotFoundException {
            return upLoad(generatePath(objType, generateFileName(file)), new FileInputStream(file));
        }

        public static String uploadToOSS(String base64, String objType) {
            if (StringUtils.isBlank(base64)) return null;
            if (base64.startsWith("http")) return base64;
            try {
                return uploadToOSS(BASE64DecodedMultipartFile.base64ToMultipart(base64), objType);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    /**
     * @方法参数 这个类下的所有方法上传的阿里云文件, 都是当前文件的文件名
     * @see #uploadToOSS(MultipartFile, String)
     */
    public static class CurrentFileName {

        public static String uploadToOSS(MultipartFile file, String objType) throws IOException {
            return upLoad(generatePath(objType, file.getOriginalFilename()), file.getInputStream());
        }
    }
}
